// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.   
// Jad home page: http://www.kpdus.com/jad.html   
// Decompiler options: packimports(3)    

package com.kingre.et.chart;

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.util.List;
import java.util.Map;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

public class BarChart {
    static class CustomRenderer extends BarRenderer {

        @Override
        public Paint getItemPaint(int i, int j) {
            return colors[j % colors.length];
        }

        private final Paint colors[];

        public CustomRenderer(Paint apaint[]) {
            colors = apaint;
        }
    }

    public static JFreeChart generateChart(String title, String xLabel, String yLabel, List<Map<String, String>> list) {
        CategoryDataset dataset = createDataset(list);
        JFreeChart chart = createChart(title, xLabel, yLabel, dataset);
        return chart;
    }

    private static CategoryDataset createDataset(List<Map<String, String>> list) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (Map<String, String> map : list) {
            dataset.addValue(Double.parseDouble(map.get("value")), "S1", map.get("vd"));
        }
        return dataset;
    }

    private static JFreeChart createChart(String title, String xLabel, String yLabel, CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createBarChart(title, // chart title
                xLabel, // domain axis label
                yLabel, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips
                false // urls
                );
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.LIGHT_GRAY);
        CategoryAxis domainAxis = plot.getDomainAxis();
        NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();

        ValueAxis rangeAxis = plot.getRangeAxis();
        // 设置最高的一个柱与图片顶端的距离
        rangeAxis.setUpperMargin(0.08);

        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.01);// 设置距离图片右端距离此时为百分之10

        CustomRenderer barRenderer = new CustomRenderer(createPaint());
        barRenderer.setItemLabelPaint(Color.black);
        barRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        barRenderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 16));
        barRenderer.setItemLabelPaint(Color.black);
        barRenderer.setItemLabelsVisible(true);
        ItemLabelPosition itemlabelposition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
        barRenderer.setPositiveItemLabelPosition(itemlabelposition);

        chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 16));
        chart.setBackgroundPaint(Color.WHITE);
        /*------设置X轴坐标上的文字-----------*/
        domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 14));
        /*------设置X轴的标题文字------------*/
        domainAxis.setLabelFont(new Font("黑体", Font.PLAIN, 16));
        /*------设置Y轴坐标上的文字-----------*/
        numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
        /*------设置Y轴的标题文字------------*/
        numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 16));
        numberaxis.setTickUnit(new NumberTickUnit(1));
        numberaxis.setRange(0D, 10D);

        barRenderer.setMaximumBarWidth(0.12);
        plot.setRenderer(barRenderer);
        return chart;
    }

    private static Paint[] createPaint() {
        Paint[] colors = new Paint[9];
        colors[0] = Color.red;
        colors[1] = Color.green;
        colors[2] = Color.blue;
        colors[3] = Color.orange;
        colors[4] = Color.magenta;
        colors[5] = Color.cyan;
        colors[6] = Color.gray;
        colors[7] = Color.pink;
        colors[8] = Color.yellow;
        return colors;
    }

}